package cn.liyunqi.mailsendandenclosure;

import cn.liyunqi.mailsendandenclosure.bo.MailConfig;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;

import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Properties;

public class MailSendAndEnclosureApplication {

    public static void main(String[] args){

        System.out.println(new Date() + "=======开始发送邮件");
        //获取配置文件地址
        String filePath = "D:\\workspace\\MailSendAndEnclosure\\MailSendAndEnclosure.config.json";


        try {
            String input = FileUtils.readFileToString(new File(filePath), "UTF-8");

            System.out.println(new Date() + "=======配置文件读取中");

            MailConfig mailConfig = JSONObject.parseObject(input, MailConfig.class);;

            System.out.println(mailConfig);


            Properties props = new Properties();
            props.put("mail.smtp.host","smtp.qq.com");
            props.put("mail.smtp.auth","true"); // 是否需要身份验证


            Session session = Session.getInstance(props, auth);
            session.setDebug(true); //主要是利于调试，默认为false

// 发件人
            Address addressFrom = new InternetAddress(mailConfig.getSendMail(), MimeUtility.encodeWord("自动发送的测试员","utf-8", "Q"));
// 收件人
            Address addressTo = new InternetAddress("", MimeUtility.encodeWord("自动发送的测试员","utf-8","Q"));



            MimeBodyPart textBodyPart=new MimeBodyPart();
            textBodyPart.setText("邮件成功发送，且无乱码"); // 邮件内容

            MimeBodyPart fileBodyPart=new MimeBodyPart();
            FileDataSource fds=new FileDataSource("");//要发送的附件地址
            fileBodyPart.setDataHandler(new DataHandler(fds));
            fileBodyPart.setFileName(fds.getName());// 设置附件的名称

            Multipart container=new MimeMultipart();
            container.addBodyPart(textBodyPart);
            container.addBodyPart(fileBodyPart);

            try{
                MimeMessage message = new MimeMessage(session);
                message.setSentDate(new Date());// 设置发送时间

                message.setSubject(PopupAuthenticator.subject,"utf-8");// 设置邮件主题的编码格式

                message.setFrom(addressFrom); // 发件人
                if(null != addressTo){
                    message.addRecipient(Message.RecipientType.TO,addressTo);// 收件人
                }
                if(null != PopupAuthenticator.cc){
                    Address[] ccAdresses = new InternetAddress[PopupAuthenticator.cc.length];
                    for (int i=0; i<PopupAuthenticator.cc.length; i++){
                        ccAdresses[i] = new InternetAddress(PopupAuthenticator.cc[i]);
                    }
// 将抄送者信息设置到邮件信息中
                    message.setRecipients(Message.RecipientType.CC, ccAdresses);
                }

                if(null != addressBCC){
//	message.addRecipient(Message.RecipientType.BCC, addressBCC);// 设置密件抄送
                }
                message.setContent(container,"utf-8"); // 添加 文本文件和附件
                message.saveChanges();

                Transport transport = session.getTransport("smtp");
// host,user,password
                transport.connect("smtp.qq.com", PopupAuthenticator.mailuser,PopupAuthenticator.password);
                Transport.send(message);

                transport.close();
            }catch (MessagingException mex) {
                System.out.println("send failed, exception: " + mex);
            }






            System.out.println(new Date() + "=======开始发送邮件成功 ==============SUCCESS");
        } catch (IOException e) {

            System.out.println(new Date() + "=======配置文件读取失败");
            e.printStackTrace();
        } catch (AddressException e) {
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }


    }
}
